<?php

namespace app\admin\model;

use app\admin\model\SystemRole;
use think\Model;
use think\model\concern\SoftDelete;

class SystemMenus extends BaseModel
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    public function getIsShowAttr($value)
    {
        $status = [0 => '禁用', 1 => '正常'];
        return $status[$value];
    }

    public static function menusTrees()
    {
        $result = SystemRole::roleToMenus();
        $data = $result[0]['menus'];
        $menus = [];
        foreach($data as $item){
            if(!$item['pid']){
                $list['id'] = $item['id'];
                $list['icon'] = $item['icon'];
                $list['menu_name'] = $item['menu_name'];
                $list['path'] = $item['path'];
                $list['path_name'] = $item['path_name'];
                $list['sort'] = $item['sort'];
                $list['children'] = self::getMenusTree($data, $item['id']);
                array_push($menus, $list);
            }
        }

        return $menus;
    }

    public static function getAllMenusList() 
    {
        $data = self::where('is_show', '=', 1)->select();
        $menus = [];
        foreach($data as $item){
            if(!$item['pid']){
                $list['id'] = $item['id'];
                $list['icon'] = $item['icon'];
                $list['menu_name'] = $item['menu_name'];
                $list['path'] = $item['path'];
                $list['path_name'] = $item['path_name'];
                $list['sort'] = $item['sort'];
                $list['children'] = self::getMenusTree($data, $item['id']);
                array_push($menus, $list);
            }
        }

        return $menus;
    }

    public static function menuCreateOrUpdate($data)
    {
        unset($data['parent_name']);
        unset($data['delete_time']);
        unset($data['link_id']);

        $model = new self;
        if (isset($data['id'])) $model = self::withTrashed()->find($data['id']);
        return $model->save($data);        
    }

    public static function menuList($param)
    {   
        $model = new self;
        if($param['search'] != 'null'){
            $model = $model->where('menu_name', 'LIKE', "%{$param['search']}%");
        }

        if($param['state'])
            $model = $model->onlyTrashed();
        else
            $model = $model->withTrashed();

        $data = $model->page($param['page'], $param['pageSize'])->order('sort', 'desc')->select();
        
        $configList = [];
        foreach($data as $item){
            $list['id'] = $item['id'];
            $list['icon'] = $item['icon'];
            $list['menu_name'] = $item['menu_name'];
            $list['path'] = $item['path'];
            $list['pid'] = $item['pid'];
            $list['is_show'] = $item['is_show'];
            $list['sort'] = $item['sort'];
            $list['path_name'] = $item['path_name'];
            $list['parent_name'] = $item['pid'] ? '': '顶级';
            $list['delete_time'] = $item['delete_time'];

            if($item['pid']){
                $parent_name = self::strMontage('parent', $data, $item['id']);
                $link_id = self::strMontage('link', $data, $item['id']);
                $list['parent_name'] = mb_substr($parent_name, 1, strlen($parent_name), 'UTF-8');
                $list['link_id'] = array_map('intval', explode(",", substr($link_id, 0, strlen($link_id) - 1)));
            }
            array_push($configList, $list);
        }
        $total = $model->count();

        return [
            'list' => $configList,
            'total' => $total
        ];
    }

    public static function delMenu($id)
    {
        $data = self::onlyTrashed()->find($id);
        if($data)
            return $data->restore();
        else
            return self::destroy($id);
    }

    public static function updateMenu($id)
    {
        $menu = new self;

        $menu = $menu::find($id);
        $menu->is_show = $menu->is_show ? 0 : 1;
        return $menu->save();
    }

    private static function getMenusTree($data, $pid)
    {
        $tree = [];
        foreach($data as $item){
            if($item['pid'] == $pid){
                $children['id'] = $item['id'];
                $children['icon'] = $item['icon'];
                $children['menu_name'] = $item['menu_name'];
                $children['path'] = $item['path'];
                $children['path_name'] = $item['path_name'];
                $children['sort'] = $item['sort'];
                $children['children'] = self::getMenusTree($data, $item['id']);
                $tree[] = $children;
            }
        }

        return $tree;
    }

    private static function strMontage($type, $data, $pid)
    {
        $str = '';
        foreach($data as $item){
            if($item['id'] == $pid){
                if($type == 'parent'){
                    $str = self::strMontage('parent',$data, $item['pid']) . '/';
                    $str .= $item['menu_name'];
                }else{
                    $str = self::strMontage('link', $data, $item['pid']);
                    $str .= $item['id'] . ',';
                }
            }
        }
        return $str;
    }
}
